Imports Microsoft.VisualBasic
Imports SubSonic
Imports ClubStarterKit.Data
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Namespace ClubStarterKit.Web.Controls
    Public Class ScoreBoxControl
        Inherits WebControl
        Implements INamingContainer

        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()

            If Not TeamID = 0 Then
                CreateTeamControls()
            End If

            If Not SeasonID = 0 Then
                CreateSeasonControls()
            End If
        End Sub

        Private m_teamid As Integer = 0
        Public Property TeamID() As Integer
            Get
                Return m_teamid
            End Get
            Set(ByVal value As Integer)
                m_teamid = value
            End Set
        End Property

        Private m_seasonid As Integer = 0
        Public Property SeasonID() As Integer
            Get
                Return m_seasonid
            End Get
            Set(ByVal value As Integer)
                m_seasonid = value
            End Set
        End Property

        Private Sub CreateSeasonControls()
            Dim rdr As IDataReader = SPs.SeasonGames(SeasonID(), True).GetReader()
            Dim gamecol As New GamesCollection()

            While rdr.Read()
                Dim gme As New Games(Games.Columns.Id, rdr.GetInt32(0))
                gamecol.Add(gme)
            End While
            rdr.Close()

            gamecol.Sort(Games.Columns.Dte, False)

            Dim game As New Games()
            Dim totalnumgames As Integer = 0

            'NOTE: game.Teams maps to Team1id (Foreign Key on Teams table) and game.TeamsToTeam2id maps to Team2id (Foreign Key on Teams table)
            Me.Controls.Add(New LiteralControl("<table width='100%'>"))

            For Each game In gamecol
                Me.Controls.Add(New LiteralControl("<tr>"))

                'Game Name
                Me.Controls.Add(New LiteralControl("<td>"))

                Dim gamename As String = game.Teams.Name
                gamename += " vs. "
                gamename += game.TeamsToTeam2id.Name
                gamename += " ("
                gamename += game.Dte.ToShortDateString()
                gamename += ")"

                Dim gamelink As New HyperLink()
                gamelink.Text = gamename
                gamelink.NavigateUrl = "~/CricketStats/game.aspx?ID=" & game.Id.ToString()
                Me.Controls.Add(gamelink)

                Me.Controls.Add(New LiteralControl("</td>"))

                'Game Result
                Dim resulttxt As String
                If Settings.IsHighestWinner = True Then
                    If game.Team1score > game.Team2score Then
                        resulttxt = game.Teams.Name & " won " & game.Team1score & "-" & game.Team2score
                    ElseIf game.Team2score > game.Team1score Then
                        resulttxt = game.TeamsToTeam2id.Name & " won " & game.Team2score & "-" & game.Team1score
                    Else
                        resulttxt = game.Teams.Name & " tied " & game.TeamsToTeam2id.Name
                    End If
                Else
                    If game.Team1score < game.Team2score Then
                        resulttxt = game.Teams.Name & " won " & game.Team1score & "-" & game.Team2score
                    ElseIf game.Team2score < game.Team1score Then
                        resulttxt = game.TeamsToTeam2id.Name & " won " & game.Team2score & "-" & game.Team1score
                    Else
                        resulttxt = game.Teams.Name & " tied " & game.TeamsToTeam2id.Name
                    End If
                End If
                Me.Controls.Add(New LiteralControl("<td>"))
                Me.Controls.Add(New LiteralControl(resulttxt))

                Me.Controls.Add(New LiteralControl("</td>"))


                Me.Controls.Add(New LiteralControl("</tr>"))

                totalnumgames += 1
            Next

            Me.Controls.Add(New LiteralControl("</table><br />"))

            Me.Controls.Add(New LiteralControl("<strong>Total Games</strong>: " & totalnumgames.ToString()))

        End Sub

        Private Sub CreateTeamControls()
            Dim win As Integer = 0
            Dim loss As Integer = 0
            Dim ties As Integer = 0

            Dim where1 As New SubSonic.Where()
            where1.ColumnName = Games.Columns.Team1id
            where1.ParameterValue = TeamID()

            Dim where2 As New SubSonic.Where()
            where2.ColumnName = Games.Columns.Team2id
            where2.ParameterValue = TeamID()
            where2.Condition = Where.WhereCondition.OR

            Dim qry As New Query(Tables.Games)
            qry.QueryType = QueryType.Select
            qry.OrderBy = OrderBy.Desc(Games.Columns.Dte)
            qry.AddWhere(where1)
            qry.AddWhere(where2)
            qry.AddWhere(Games.Columns.IsComplete, True)

            Dim gamecol As New GamesCollection()
            gamecol.LoadAndCloseReader(qry.ExecuteReader())

            Dim game As New Games()

            'NOTE: game.Teams maps to Team1id (Foreign Key on Teams table) and game.TeamsToTeam2id maps to Team2id (Foreign Key on Teams table)
            Me.Controls.Add(New LiteralControl("<table width='100%'>"))

            For Each game In gamecol
                Me.Controls.Add(New LiteralControl("<tr>"))

                'Game Name
                Me.Controls.Add(New LiteralControl("<td>"))

                Dim gamename As String = game.Teams.Name
                gamename += " vs. "
                gamename += game.TeamsToTeam2id.Name
                gamename += " ("
                gamename += game.Dte.ToShortDateString()
                gamename += ")"

                Dim gamelink As New HyperLink()
                gamelink.Text = gamename
                gamelink.NavigateUrl = "~/CricketStats/game.aspx?ID=" & game.Id.ToString()
                Me.Controls.Add(gamelink)

                Me.Controls.Add(New LiteralControl("</td>"))

                'Game Result
                Dim resulttxt As String
                If Settings.IsHighestWinner = True Then
                    If game.Team1score > game.Team2score Then
                        resulttxt = game.Teams.Name & " won " & game.Team1score & "-" & game.Team2score
                        If game.Team1id = TeamID() Then
                            win += 1
                        Else
                            loss += 1
                        End If
                    ElseIf game.Team2score > game.Team1score Then
                        resulttxt = game.TeamsToTeam2id.Name & " won " & game.Team2score & "-" & game.Team1score
                        If game.Team2id = TeamID() Then
                            win += 1
                        Else
                            loss += 1
                        End If
                    Else
                        resulttxt = game.Teams.Name & " tied " & game.TeamsToTeam2id.Name
                        ties += 1
                    End If
                Else
                    If game.Team1score < game.Team2score Then
                        resulttxt = game.Teams.Name & " won " & game.Team1score & "-" & game.Team2score
                        If game.Team1id = TeamID() Then
                            win += 1
                        Else
                            loss += 1
                        End If
                    ElseIf game.Team2score < game.Team1score Then
                        resulttxt = game.TeamsToTeam2id.Name & " won " & game.Team2score & "-" & game.Team1score
                        If game.Team2id = TeamID() Then
                            win += 1
                        Else
                            loss += 1
                        End If
                    Else
                        resulttxt = game.Teams.Name & " tied " & game.TeamsToTeam2id.Name
                        ties += 1
                    End If
                End If
                Me.Controls.Add(New LiteralControl("<td>"))
                Me.Controls.Add(New LiteralControl(resulttxt))

                Me.Controls.Add(New LiteralControl("</td>"))


                Me.Controls.Add(New LiteralControl("</tr>"))
            Next

            Me.Controls.Add(New LiteralControl("</table>"))

            Dim record As String
            record = "<strong>Wins</strong>: " & win
            record += "  <strong>Losses</strong>: " & loss
            If ties > 0 Then
                record += " <strong>Ties</strong>: " & ties
            End If

            Me.Controls.Add(New LiteralControl("<br />"))
            Me.Controls.Add(New LiteralControl(record))
        End Sub
    End Class

End Namespace